package model

import (
	"gorm.io/gorm"

	__ "user-srv/basic/proto"
)

type Video struct {
	gorm.Model
	UserId int    `gorm:"type:int(11);not null"`
	Title  string `gorm:"type:varchar(255);not null"`
	Image  string `gorm:"type:varchar(255);not null"`
	Info   string `gorm:"type:varchar(100);not null"`
	Num    int    `gorm:"type:int(11);not null"`
	Types  int    `gorm:"type:int(11);not null"`
	Status int    `gorm:"type:int(11);not null"`
}

func (v *Video) GetOneFile(db *gorm.DB, title string) error {
	return db.Model(&v).Where("title = ?", title).Limit(1).Find(&v).Error
}
func (v *Video) Create(db *gorm.DB) error {
	return db.Model(&v).Create(&v).Error
}
func (v *Video) GetUpdate(db *gorm.DB, id int) error {
	return db.Model(&v).Where("id = ?", id).Updates(&v).Error
}
func (v *Video) Deleted(db *gorm.DB, id int) error {
	return db.Model(&v).Where("id = ?", id).Delete(&v).Error
}
func (v *Video) Show(db *gorm.DB, id int) error {
	return db.Model(&v).Where("id = ?", id).Limit(1).Find(&v).Error
}
func (v *Video) GetCover(db *gorm.DB, Page, size int) (videolist []*__.Videolist, err error) {
	page := Page
	if page <= 0 {
		page = 1
	}
	pageSize := size
	switch {
	case pageSize > 100:
		pageSize = 100
	case pageSize <= 0:
		pageSize = 10
	}
	offset := (page - 1) * pageSize
	err = db.Model(&v).Order("created_at desc").Offset(offset).Limit(pageSize).Find(&videolist).Error
	return
}
